﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Layer;
using DTO;

public partial class NNC_H_ChangePass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string userName = Session["NCC_UN"].ToString();
            TextBox1.Text = userName;
        }
    }
    NhaCungCapBUS nccBUS = new NhaCungCapBUS();
    NhaCungCapDTO nccDTO = new NhaCungCapDTO();
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("QL_TaiKhoanNCC.aspx");
    }
    public string GetMD5Hash(string sPassword)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashedDataBytes = md5Hasher.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sPassword));
        string sEncryptPass = Convert.ToBase64String(hashedDataBytes);
        return sEncryptPass;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {


        IList<NhaCungCapDTO> list = nccBUS.GetList(TextBox1.Text);
        for (int i = 0; i < list.Count(); i++)
        {
            TextBox2.Text = list[i].MatKhau;

            nccDTO.DiaChi = list[i].DiaChi;
            nccDTO.SoDT = list[i].SoDT;
            nccDTO.Ten = list[i].Ten;
            nccDTO.TenDN = list[i].TenDN;
            nccDTO.TenNH1 = list[i].TenNH1;
            nccDTO.TenNH2 = list[i].TenNH2;
            nccDTO.SoTK1 = list[i].SoTK1;
            nccDTO.SoTk2 = list[i].SoTk2;
            nccDTO.WSurl = list[i].WSurl;
            nccDTO.ID = list[i].ID;
            nccDTO.QuocGia = list[i].QuocGia;
        }
        if (GetMD5Hash(TextBox3.Text) != GetMD5Hash(TextBox4.Text))
        {
            Response.Write("<script> alert('Mật khẩu không đúng!!!!Vui lòng xem lai.')</script>");
        }
        else
        {
            nccDTO.MatKhau = GetMD5Hash(TextBox3.Text);

        }
        bool kiemtraud = nccBUS.updateSuppler(nccDTO);
        if (kiemtraud == true)
        {
            Response.Write("<script> alert('Đã thay đổi mật khẩu thành công !!!.')</script>");
          
        }
        else
        {
            Response.Write("<script> alert('Thay đổi mật khẩu không thành công !!!.')</script>");
        }
    }
}